package fuzion24.device.vulnerability.util;

import android.content.Context;
import android.content.SharedPreferences;

import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

public class SharedPreferencesUtils {

    private static final String PREFS_LAST_LIST_OF_SCANS = "PREFS_LIST_OF_SCANS";
    private static final String PREFS_FIRST_RUN = "PREFS_FIRST_RUN";
    private static final String PREFS_AUTOMATIC_SHARING_ENABLE = "PREFS_AUTOMATIC_SHARING_ENABLE";
    private static final String BUILD_FINGERPRINT_HASH   = "BUILD_FINGERPRINT_HASH";


    private static final String PREFS_NAME = "com.nowsecure.android.vts.PREFERENCE_FILE_KEY";

    private SharedPreferencesUtils() {
    }

    private static SharedPreferences getPreferences(Context context) {
        return context.getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE);
    }

    public static Boolean isAutomaticSharingEnable(Context context) {
        return  getPreferences(context).getBoolean(PREFS_AUTOMATIC_SHARING_ENABLE, false);
    }

    public static void setIsAutomaticSharingEnable(Context context, Boolean isAutomaticSharingEnable) {
        getPreferences(context).edit().putBoolean(PREFS_AUTOMATIC_SHARING_ENABLE, isAutomaticSharingEnable).commit();
    }

    public static Boolean isTheFirstRun(Context context) {
        return getPreferences(context).getBoolean(PREFS_FIRST_RUN, true);
    }

    public static void disableFirstRun(Context context) {
        getPreferences(context).edit().putBoolean(PREFS_FIRST_RUN, false).commit();
    }


    public static List<String> getTheListOfScansAvailable(Context context) {
        Set<String> listOfPreviousScans = getPreferences(context).getStringSet(PREFS_LAST_LIST_OF_SCANS, null);

        if (listOfPreviousScans == null) {
            return null;
        }

        List<String> listOfScans = Arrays.asList(listOfPreviousScans.toArray(new String[listOfPreviousScans.size()]));

        return listOfScans;
    }

    public static void setTheListOfScansAvailable(Context context, List<String> listOfScans) {
        SharedPreferences.Editor editor = getPreferences(context).edit();

        editor.putStringSet(PREFS_LAST_LIST_OF_SCANS, new HashSet<>(listOfScans));

        editor.apply();
    }

    public static void setBuildUpdateFingerPrint(Context context, String fingerprint){
        SharedPreferences.Editor editor = getPreferences(context).edit();

        editor.putString(BUILD_FINGERPRINT_HASH, fingerprint);

        editor.apply();
    }

    public static String getBuildUpdateFingerprint(Context context){
        return getPreferences(context).getString(BUILD_FINGERPRINT_HASH, null);
    }

}
